
import axios from 'axios'
import { showLoadingToast, closeToast } from 'vant'
import 'vant/es/toast/style'
let host
// 判断一下环境
if (process.env.NODE_ENV === 'development') {
  host = 'http://shop.bufantec.com/bufan'
} else {
  host = 'http://shop.bufantec.com/bufan'
}

// 创建实例

const instance = axios.create({
  baseURL: host,
  timeout: 5 * 1000
})

// 请求拦截器
instance.interceptors.request.use(config => {
  showLoadingToast({
    message: '加载中...',
    forbidClick: true
  })
  // get和post请求参数统一处理
  if (config.method.toLowerCase() === 'get') {
    config.params = config.data
  }
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截器
instance.interceptors.response.use(respose => {
  //
  closeToast()
  return respose.data
}, error => {
  return Promise.reject(error)
})
/**
function request (url, method, data) {
  return instance({
    url,
    method,
    // get :params ;post:data
    // 属性:params?data?
    [method === 'get' ? 'params' : 'data']: data
  })
}
*/
// export default request
// request('httpxxx', 'get', { id: '1234565' })
export default instance
